/**
 * (created at 2011-1-26)
 */
package qiushuo.mysqlparser.ast.expression.misc;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import qiushuo.mysqlparser.ast.expression.Expression;
import qiushuo.mysqlparser.generator.SQLType;

/**
 * @author <a href="mailto:QiuShuo1985@gmail.com">QIU Shuo</a>
 */
public class InExpressionList implements Expression {
    private final List<Expression> list;

    public InExpressionList(List<Expression> list) {
        if (list == null || list.size() == 0) {
            this.list = Collections.emptyList();
        } else if (list instanceof ArrayList) {
            this.list = list;
        } else {
            this.list = new ArrayList<Expression>(list);
        }
    }

    @Override
    public String generateSql(SQLType type) {
        StringBuilder sb = new StringBuilder();
        sb.append('(');
        boolean isFst = true;
        for (Expression expr : list) {
            if (isFst) {
                isFst = false;
            } else {
                sb.append(", ");
            }
            sb.append(expr.generateSql(type));
        }
        sb.append(')');
        return sb.toString();
    }

    @Override
    public int getPrecedence() {
        return PRECEDENCE_PRIMARY;
    }
}
